package com.gange.springboot.controller;

import com.gange.springboot.entity.ResultEntity;
import com.gange.springboot.entity.User;
import com.gange.springboot.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController //该类就是控制器类，可以返回前端数据
public class UserController {
    @Autowired
    private UserService userService; //让spring把业务类的对象注入到当前类中  userSerice =  new UserSericeImpl()
    @GetMapping("/login")
    public ResultEntity<User> login(@RequestParam("name") String name, @RequestParam("password") String pwd,
                                    //响应对象
                                    HttpServletResponse response,HttpSession session){
        User user = userService.login(name,pwd);
        if(user==null){
            return ResultEntity.error("账号密码错误！");
        }else{
//            Cookie cookie  = new Cookie("username",name);
//            cookie.setMaxAge(60);
//            //把生成的cookie给浏览器，让浏览器保存
//            response.addCookie(cookie);
            session.setAttribute("flag",true);
            return ResultEntity.ok(user);
        }
    }
    //前端传递user的时候要以json的格式传递  {"name":"zs","password":"123"}
    //RequestBody表示把前端的json对象转换为java的对象
    @PostMapping("/add/user")
    public void addUser(@RequestBody User user){
        userService.insert(user);
    }
    @DeleteMapping("/delete/{id}")
    public ResultEntity deleteUser(@PathVariable("id") int id){
        try {
            int delete = userService.delete(id);
            if(delete<1){
                return ResultEntity.error("没有查询该数据，删除失败！");
            }
        }catch (Exception e){
            return ResultEntity.error(e.getMessage());
        }
        return ResultEntity.ok();
    }
    @PutMapping("/update")
    public ResultEntity<User> updateUser(@RequestBody User user){
        userService.update(user);
        return ResultEntity.ok(user);
    }
    @GetMapping("/user/{id}")
    public ResultEntity<User>  selectById(@PathVariable("id") int id){
        return ResultEntity.ok(userService.selectById(id));
    }

    @GetMapping("/users")
    public ResultEntity<List<User>> selectAll(HttpServletRequest request){
        /*//cookie一旦保存到客户端后，浏览器就自动保存了。浏览器发请求的时候自动会携带当前网站的所有cookie
        //获取cookie
        Cookie[] cookies = request.getCookies();//获取到网站保存到浏览器的所有cookie信息
        if(cookies!=null){
            for(Cookie cookie:cookies){
                String name = cookie.getName(); //username
                String value = cookie.getValue();//zhangsan
                System.out.println("name:"+name+";value:"+value);
            }
        }*/
        HttpSession session = request.getSession();
        System.out.println(session.getId());
        Boolean flag = (Boolean) session.getAttribute("flag");
        if(flag!=null&&flag) {
            return ResultEntity.ok(userService.selectAll());
        }else{
            return ResultEntity.error("没有登录！");
        }
    }
    @DeleteMapping("/logout")
    public ResultEntity<String> logout(HttpSession session){
        //让session失效，默认30分钟session失效
        session.invalidate();
        return ResultEntity.ok("退出成功");
    }
}
